package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.dto.AlertRuleQueryDto;
import com.zzyl.entity.AlertRule;
import com.zzyl.mapper.AlertRuleMapper;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlertRuleServiceImpl implements AlertRuleService {

    @Autowired
    private AlertRuleMapper alertRuleMapper;


    @Override
    public void create(AlertRuleDto dto) {
        AlertRule alertRule = BeanUtil.toBean(dto, AlertRule.class);
        alertRuleMapper.insert(alertRule);
    }

    @Override
    public PageBean<AlertRuleVo> queryPage(AlertRuleQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<AlertRuleVo> page = alertRuleMapper.page(dto);
        return PageBean.of(page,AlertRuleVo.class);
    }

    @Override
    public AlertRuleVo getById(Long id) {
        AlertRule alertRule = alertRuleMapper.selectByPrimaryKey(id);
        return BeanUtil.toBean(alertRule,AlertRuleVo.class);

    }

    @Override
    public void update(AlertRuleDto dto) {
        AlertRule alertRule = BeanUtil.toBean(dto, AlertRule.class);
        alertRuleMapper.updateByPrimaryKeySelective(alertRule);
    }

    @Override
    public void delete(Long id) {
        AlertRule alertRule = alertRuleMapper.selectByPrimaryKey(id);
        if (alertRule.getStatus() == 1) {
            throw new RuntimeException("该规则启用中,无法删除!");
        }
        alertRuleMapper.deleteByPrimaryKey(id);
    }



    @Override
    public void startOrStop(Long id, Integer status) {
        alertRuleMapper.updateStatus(id,status);
    }
}
